/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.dakaqi.entities;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Cacheable
@Entity
@Table(name = "U_USER")
public class User extends IdEntity {

	private String mobile;
	private String email;
	private String nickName;
	private String realName;
	private String password;
	private String roles;
	private Date createTime;
	private int platform;
	private int delStatus;
	private int locked;
	private long oldID;
	private String oldMemberCode;
	private String area;
	private double lng;
	private double lat;

	public User() {
	}

	public User(Long id) {
		this.id = id;
	}

	public String getArea()
	{
		return area;
	}

	public void setArea(String area)
	{
		this.area = area;
	}

	public double getLng()
	{
		return lng;
	}

	public void setLng(double lng)
	{
		this.lng = lng;
	}

	public double getLat()
	{
		return lat;
	}

	public void setLat(double lat)
	{
		this.lat = lat;
	}

	public long getOldID()
	{
		return oldID;
	}

	public void setOldID(long oldID)
	{
		this.oldID = oldID;
	}

	public String getOldMemberCode()
	{
		return oldMemberCode;
	}

	public void setOldMemberCode(String oldMemberCode)
	{
		this.oldMemberCode = oldMemberCode;
	}

	@NotBlank
	public String getMobile()
	{
		return mobile;
	}

	public void setMobile(String mobile)
	{
		this.mobile = mobile;
	}
	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getNickName()
	{
		return nickName;
	}

	public void setNickName(String nickName)
	{
		this.nickName = nickName;
	}
	public String getRealName()
	{
		return realName;
	}

	public void setRealName(String realName)
	{
		this.realName = realName;
	}

	@NotBlank
	@JsonIgnore
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public String getRoles() {
		return roles;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	@Transient
	@JsonIgnore
	public List<String> getRoleList() {
		// 角色列表在数据库中实际以逗号分隔字符串存储，因此返回不能修改的List.
		if(StringUtils.isBlank(roles))
		{
			List<String> list = new ArrayList<String>();
			list.add("user");
			return list;
		}
		else
			return ImmutableList.copyOf(StringUtils.split(roles, ","));
	}

	// 设定JSON序列化时的日期格式
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getCreateTime()
	{
		return createTime;
	}

	public void setCreateTime(Date createTime)
	{
		this.createTime = createTime;
	}

	public int getLocked()
	{
		return locked;
	}

	public void setLocked(int locked)
	{
		this.locked = locked;
	}

	public int getPlatform()
	{
		return platform;
	}

	public void setPlatform(int platform)
	{
		this.platform = platform;
	}

	public int getDelStatus()
	{
		return delStatus;
	}

	public void setDelStatus(int delStatus)
	{
		this.delStatus = delStatus;
	}

	//    @Override
//    public String toString() {
//        return ToStringBuilder.reflectionToString(this);
//    }
}